Ext.define('Seq.data.TableChartController', {
    extend: 'Seq.app.BaseController',
    alias: 'controller.tablechart',

    matrixOfInterval: function (interval) {
        if (!Ext.isNumber(interval)) {
            return eval('this.' + interval + 'Matrix()');
        }
    },

    onPrevClick: function (btn) {
        let me = this, vm = me.getViewModel(), date = vm.get('date'), pv = Seq.Date.joda2pv(vm.get('interval'));
        vm.set('date', Ext.util.Format.date(Ext.Date.add(new Date(date), pv.p, -pv.v), 'Y-m-d'));
    },

    onNextClick: function (btn) {
        let me = this, vm = me.getViewModel(), date = vm.get('date'), pv = Seq.Date.joda2pv(vm.get('interval'));
        vm.set('date', Ext.util.Format.date(Ext.Date.add(new Date(date), pv.p, pv.v), 'Y-m-d'));
    },

    onIntervalChange: function (cmp, newValue, oldValue, opts) {
        if (!Ext.isEmpty(newValue) && !Ext.isEmpty(oldValue) && (newValue !== oldValue)) {
            let me = this, view = me.getView(), table = view.queryById('table');
            table.getView().getStore().removeAll();
            // reconfigure
            let matrix = me.matrixOfInterval(newValue);
            matrix && table.getMatrix().reconfigure(matrix);
            // frefresh
            let params = {};
            params[cmp.getBind().value.stub.name] = newValue;
            view.fireEvent('refreshdata', view, me.getCr(), params, {});
        }
    },

    onCardChange: function (cmp, newValue, oldValue, opts) {
        if (!Ext.isEmpty(newValue) && !Ext.isEmpty(oldValue) && (newValue !== oldValue)) {
            let me = this, params = {}, view = me.getView().queryById(newValue);
            params[cmp.getBind().value.stub.name] = newValue;
            view.fireEvent('refreshdata', view, me.getCr(), params, opts);
        }
    },

    onRefreshData(view, record, params, opts) {
        let me = this, vm = me.getViewModel(), card = vm.get('card'), table = view.queryById(card);
        if ('table' == card) {
            let matrix = table.getMatrix();
            if (Ext.isEmpty(matrix.leftAxis.dimensions.items)) { // 第一次初始化table
                matrix.reconfigure(me.matrixOfInterval(vm.get('interval')));
            }
        }
        table.fireEvent('refreshdata', table, record, params, opts);
    },

    onRefreshTable(view, record, params, opts) {
        // 强制清除pivot.getView().getStore()的数据
        let me = this, store = view.getView().getStore();
        store && store.removeAll();
        // 重新加载数据
        me.loadStores([view.getBind().store.stub.name], params, opts);

    },

    onRefreshChart(view, record, params, opts) {
        var me = this, url = {
            url: view.echartUrl,
            params: me.getExtraParams(params)
        }
        me.getViewModel().set('echartUrl', url);
    },

    p1dMatrix() {
        return {
            leftAxis: [{
                dataIndex: 'rowName',
                sortIndex: 'rowId',
                width: 85,
                align: 'center'
            }],
            topAxis: [{
                dataIndex: 'colName',
                sortIndex: 'colId',
                align: 'center'
            }]
        }
    },

    p7dMatrix() {
        return this.p1dMatrix();
    },

    p1wMatrix() {
        return this.p1dMatrix();
    },

    p15dMatrix() {
        return this.p1dMatrix();
    },
    p1mMatrix() {
        return this.p1dMatrix();
    },
    p1yMatrix() {
        return this.p1dMatrix();
    }

});